package com.stiff.manage.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.abel533.entity.Example;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stiff.common.service.ApiService;
import com.stiff.manage.mapper.ItemMapper;
import com.stiff.manage.pojo.Item;
import com.stiff.manage.pojo.ItemDesc;
import com.stiff.manage.pojo.ItemParam;
import com.stiff.manage.pojo.ItemParamItem;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ItemService extends BaseService<Item> {
    @Autowired
    private ItemDescService itemDescService;
    @Autowired
    private ItemParamItemService itemParamItemService;
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ApiService apiService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Value("${TAOTAO_WEB_URL}")
    private String TAOTAO_WEB_URL;
    private static final ObjectMapper MAPPER = new ObjectMapper();
    public void saveItem(Item item, String desc,String itemParams) {
        item.setStatus(1);
        item.setId(null);
        super.save(item);
        ItemDesc itemDesc = new ItemDesc();
        itemDesc.setItemId(item.getId());
        itemDesc.setItemDesc(desc);
        this.itemDescService.save(itemDesc);
        ItemParamItem itemParamItem = new ItemParamItem();
        itemParamItem.setItemId(item.getId());
        itemParamItem.setParamData(itemParams);
        this.itemParamItemService.save(itemParamItem);
        sendMsg(item.getId(),"insert");
    }

    public PageInfo<Item> queryPageList(Integer page, Integer rows) {
        Example example = new Example(Item.class);
        example.setOrderByClause("updated DESC");
        PageHelper.startPage(page,rows);
        List<Item> list = this.itemMapper.selectByExample(example);
        return new PageInfo<>(list);
    }

    public void deleteItemByIds(List<Object> ids) {
        super.deleteByIds(Item.class,"id",ids);
        this.itemDescService.deleteByIds(ItemDesc.class,"itemId",ids);
    }

    public void updateItem(Item item, String desc,String itemParams) {
        item.setStatus(null);
        item.setCreated(null);
        super.updateSelective(item);
        ItemDesc itemDesc = new ItemDesc();
        itemDesc.setItemDesc(desc);
        itemDesc.setItemId(item.getId());
        this.itemDescService.updateSelective(itemDesc);
        this.itemParamItemService.updateItemParamItem(item.getId(),itemParams);
        //通知其他系统，该商品已经更新
//        try {
//            String url = TAOTAO_WEB_URL+"/item/cache/"+item.getId()+".html";
//            this.apiService.doPost(url,null);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        sendMsg(item.getId(),"update");
    }

    public void updateItemsStatus(List<Object> idList, int itemStatus) {
        Item item = new Item();
        item.setStatus(itemStatus);
        Example example = new Example(Item.class);
        example.createCriteria().andIn("id",idList);
        this.itemMapper.updateByExampleSelective(item,example);
        sendMsg(item.getId(),"update");
    }

    private void sendMsg(Long itemId,String type){
        try {
            Map<String,Object> msg = new HashMap<>();
            msg.put("itemId",itemId);
            msg.put("type",type);
            msg.put("data",System.currentTimeMillis());
            rabbitTemplate.convertAndSend("item."+type,MAPPER.writeValueAsString(msg));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
